/**
 * webAlert 工具类，封装常用一些弹窗方法，此工具类依赖webdialog框架
 * 路径为/js/plugins/webdialog/webdialog.min.js
 * Created by zhangsi on 2015/10/12.
 */
(function(window, undefined ){
    var util = function(){};
    /**
     * 简单弹窗，最常用警告弹窗
     * @param msg 弹窗消息 默认为空
     * @param title 弹窗标题 默认为提示
     * @param bgColor 弹窗背景颜色
     * @param width 弹窗宽度，默认为 300
     * @param height 弹窗高度，默认为10
     * @param closeName 弹窗关闭按钮名称，默认为关闭
     */
    util.prototype.simpleAlert = function(msg, title, bgColor, width, height, closeName){
       return webAlert({
            title: title || "提示",
            content: msg,
            opacity:.5,
            bg: bgColor || "gray",
            width: width || 300,
            height: height || 10,
            button:[
                {
                    name: closeName || "关闭",
                    callback:function(){
                        this.close();
                    }
                }]
        });
    }
    /**
     * 带关闭回调的simpleAlert
     */
    util.prototype.simpleAlertWithCloseCallBack = function(msg, title, closeFn){
    	return webAlert({
            title: title || "提示",
            content: msg,
            opacity:.5,
            bg: "gray",
            width: 300,
            height: 10,
            closeFn: closeFn,
            button:[
                {
                    name: "关闭",
                    callback:function(){
                        this.close();
                    }
                }]
        });
    }
    
    
    /**
     * 判断是否为function类型，是返回本身，否返回默认dFunction，没有默认返回空function
     */
    util.prototype.retufnFunction = function(tFunction,dFunction){
        if (typeof tFunction == 'function') {
            return tFunction;
        }
        if (typeof dFunction == 'function') {
            return dFunction;
        }
        return function(){};
    }
    /***
     * 简单的确认弹窗，其他参数类似 simpleAlert
     * @param msg 需要弹出的消息
     * @param yes 点击确定时候回掉
     * @param no 点击取消时候回掉
     */
    util.prototype.simpleConfrimAlert = function(msg, yes, no, title, bgColor, width, height) {
        yes = this.retufnFunction(yes);
        no = this.retufnFunction(no);
        return webAlert({
            title: title || "提示",
            content: msg,
            opacity: .5,
            bg: bgColor || "gray",
            width: width || 300,
            height: height || 10,
            button:[
                {
                    name:"确定",
                    callback:function(){
                        yes();
                        this.close();
                    }
                },{
                    name:"取消",
                    callback:function(){
                        no();
                        this.close();
                    }
                }
            ]
        });
    }
    /**
     * 简单消息提醒框
     * @param title 标题
     * @param content 内容
     */
    util.prototype.alert = function(title,content) {
    	return webAlert({
            title:'提示',
            content:content,
            opacity:.5,
            bg:"gray",
            width:300,
            height:10,
            button:[
                    {
                    name:"关闭",
                    callback:function(){
                  	  this.close();
                    }
                    }]
            });
    }
    /***
     * 带回掉确认弹窗
     * @param msg 需要弹出的消息
     * @param yes 点击确定时候回掉
     * @param no 点击取消时候回掉
     */
    util.prototype.callbackConfrimAlert = function(msg, yes, title, bgColor, width, height) {
        yes = this.retufnFunction(yes);
        return webAlert({
            title: title || "提示",
            content: msg,
            opacity: .5,
            bg: bgColor || "gray",
            width: width || 300,
            height: height || 10,
            button:[
                {
                    name:"确定",
                    callback:function(){
                        yes();
                        this.close();
                    }
                }
            ]
        });
    }
    
    /**
     * loading动画
     */
    util.prototype.loading = function(title,width,height) {
    	return webAlert({
    		title: title,
    		width: width || 300,
            height: height || 10
    	});
    }


    //加载进度提示
    util.prototype.loadDynamic = function(){
        var percent = 0;
        var notice = new PNotify({
            text: "Please Wait",
            type: 'info',
            icon: 'fa fa-spinner fa-spin',
            hide: false,
            buttons: {
                closer: false,
                sticker: false
            },
            opacity: .75,
            shadow: true,
            width: "170px"
        });

        setTimeout(function() {
            notice.update({
                title: false
            });
            var interval = setInterval(function() {
                percent += 2;
                var options = {
                    text: percent + "% complete."
                };
                if (percent == 80) options.title = "Almost There";
                if (percent >= 100) {
                    window.clearInterval(interval);
                    options.title = "Done!";
                    options.type = "success";
                    options.hide = true;
                    options.buttons = {
                        closer: true,
                        sticker: true
                    };
                    options.icon = 'fa fa-check';
                    options.opacity = 1;
                    options.shadow = true;
                    options.width = PNotify.prototype.options.width;
                }
                notice.update(options);
            }, 120);
        }, 2000);

    }

    //操作成功toast
    util.prototype.toast = function(obj){
        new PNotify({
            title: obj.title,
            text: obj.text ? obj.text : '',
            type: obj.type ? obj.type : 'notice',
            delay: 2000
        });
    }

    //顶部提示信息
    util.prototype.bigNotice = function(type) {
        new PNotify({
            title: '提示信息',
            text: msg,
            width: '500px',
            min_height: '400px'
        });
    }

    window.webDialogUtil = new util();
})(window);
